package com.job.client.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.job.client.common.result.AppHttpCode;
import com.job.client.common.result.ResponseResult;
import com.job.client.message.mapper.NoticeMapper;
import com.job.client.message.service.NoticeService;
import com.job.client.model.message.constant.NoticeConstant;
import com.job.client.model.message.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Override
    public ResponseResult getAll(String userId,Integer page,Integer size) {
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(page == null){
            page = 1;
        }
        if(size == null){
            size = 20;
        }
        Page<Notice> data = new Page<>(page,size);
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",userId).eq("is_delete",0);
        noticeMapper.selectPage(data,updateWrapper);
        return ResponseResult.OkResult(data);
    }
    @Override
    public ResponseResult unread(String userId,Integer page,Integer size) {
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(page == null){
            page = 1;
        }
        if(size == null){
            size = 20;
        }
        Page<Notice> data = new Page<>(page,size);
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_read", NoticeConstant.UNREAD).eq("is_delete",0).eq("user_id",userId);
        noticeMapper.selectPage(data,queryWrapper);
        return ResponseResult.OkResult(data);
    }
    @Override
    public ResponseResult read(String userId,Integer page,Integer size) {
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(page == null){
            page = 1;
        }
        if(size == null){
            size = 20;
        }
        Page<Notice> data = new Page<>(page,size);
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_read", NoticeConstant.READ).eq("is_delete",0).eq("user_id",userId);
        noticeMapper.selectPage(data,queryWrapper);
        return ResponseResult.OkResult(data);
    }
    @Override
    public ResponseResult setRead(String noticeId) {
        if(noticeId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(noticeId.charAt(0)=='"'){
            noticeId = noticeId.substring(1,noticeId.length()-1);
        }
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_read",NoticeConstant.READ).eq("notice_id",noticeId);
        int i = noticeMapper.update(updateWrapper);
        if(i!=1){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult setReadBluk(List<String> noticeIdList) {
        if(noticeIdList == null || noticeIdList.size() == 0){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        for (String noticeId : noticeIdList) {
            UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("is_read",NoticeConstant.READ).eq("notice_id",noticeId);
            noticeMapper.update(updateWrapper);
        }
        return ResponseResult.OkResult();
    }


    @Override
    public ResponseResult deleteBluk(List<String> noticeIdList) {
        if(noticeIdList == null || noticeIdList.size()==0){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        noticeMapper.deleteBatchIds(noticeIdList);
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult getCount(String userId) {
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_read",NoticeConstant.UNREAD).eq("is_delete",0).eq("user_id",userId);
        Long count = noticeMapper.selectCount(queryWrapper);
        return ResponseResult.OkResult(count);
    }

}
